package com.czxy.enterprise.config;

;
import com.alibaba.fastjson.JSON;
import com.czxy.domain.TbUser;
import com.czxy.enterprise.feign.UserFeign;
import com.czxy.vo.GoEasyVo;
import com.czxy.vo.Notification;
import com.czxy.vo.Sender;
import com.czxy.vo.To;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
public class GoEasyPublish {
    @Resource
    private UserFeign userFeign;
    public void Publish(String id,String content ){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost("http://rest-hangzhou.goeasy.io/v2/im/message");
        TbUser data = userFeign.findById(Integer.parseInt(id)).getData();
        GoEasyVo goEasyVo = new GoEasyVo();
        Sender sender = new Sender();
        goEasyVo.setAppkey("BC-e1233f64c0164fc6b82e3ebd70dbc531");
        goEasyVo.setSenderId("3");
        sender.setNickname("找活儿官方");
        sender.setName("找活儿官方");
        sender.setAvatar("https://findjob-xw.oss-cn-shanghai.aliyuncs.com/findJob/1645960870241.png");
        goEasyVo.setSenderData(sender);
        To to= new To();
        to.setType("private");
        to.setId(id);
        Sender sender1 = new Sender();
        sender1.setAvatar(data.getPhoto().getPhotoUrl());
        sender1.setName(data.getUsername());
        to.setData(sender1);
        goEasyVo.setTo(to);
        goEasyVo.setType("text");
        goEasyVo.setPayload(content);
        Notification notification = new Notification();
        notification.setTitle(content);
        notification.setBody("你有一条新消息");
        goEasyVo.setNotification(notification);
        String goEasyStr = JSON.toJSONString(goEasyVo);
        StringEntity stringEntity = new StringEntity(goEasyStr, "UTF-8");
        httpPost.setEntity(stringEntity);
        System.out.println(goEasyStr);
        httpPost.setHeader("Content-Type","application/json;charset=utf8");
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            System.out.println("发送状态为: "+response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
